home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Scripting the Finder / Finder Tricks / StringUtilities.cp < prev    next >
Encoding:
Text File  |  1994-10-04  |  6.9 KB  |  276 lines  |  [TEXT/MMCC]

  1. /*================================================================================
  2.     stringUtilities.c
  3.     
  4.     ©1991-2 Greg Anderson
  5.     greggor@apple.com
  6.     
  7.     Convienient string utility routines.  See the macros defined
  8.     in stringUtilities.h, too.
  9. ================================================================================*/
  10. #include "StringUtilities.h"
  11. #include <string.h>
  12.  
  13. #ifndef __TYPES__
  14. #include <Types.h>
  15. #endif
  16.  
  17. /*----------------------------------------------------------------------
  18. // PtoCcpy
  19. //
  20. // Copy a Pascal string into a C string
  21. ----------------------------------------------------------------------*/
  22. void PtoCcpy( char* cstr, Str255 pstr )
  23. {
  24.     unsigned short        i = (unsigned char)pstr[0];
  25.     
  26.     cstr[i] = 0;
  27.     while( i > 0 )
  28.     {
  29.         cstr[i - 1] = pstr[i];
  30.         --i;
  31.     }
  32. }
  33.  
  34. /*----------------------------------------------------------------------
  35. // CtoPcpy
  36. //
  37. // Copy a C string into a Pascal string
  38. ----------------------------------------------------------------------*/
  39. void CtoPcpy(  Str255 pstr, char* cstr )
  40. {
  41.     int        i = 1;
  42.     
  43.     while( (*cstr) && (i < 255) )
  44.         pstr[i++] = *cstr++;
  45.     pstr[0] = i - 1;
  46. }
  47.  
  48. /*----------------------------------------------------------------------
  49. // PtoCcmp
  50. //
  51. // Compare a Pascal string with a C string
  52. ----------------------------------------------------------------------*/
  53. short PtoCcmp( Str255 pstr, char* cstr )
  54. {
  55.     char        tmp[256];
  56.     
  57.     PtoCcpy( tmp, pstr );
  58.     return( strcmp( tmp, cstr ) );
  59. }
  60.  
  61. /*----------------------------------------------------------------------
  62. // pstrcpy
  63. //
  64. // Copy one pascal string into another
  65. ----------------------------------------------------------------------*/
  66. pascal void pstrcpy( Str255 dest, Str255 src )
  67. {
  68.     unsigned char*        dp;
  69.     unsigned char*        sp;
  70.     short                i;
  71.     short                max;
  72.     
  73.     sp = (unsigned char*)src;
  74.     dp = (unsigned char*)dest;
  75.     max = *sp;
  76.     
  77.     for( i = 0; i <= max ; ++i )
  78.         *dp++ = *sp++;
  79. }
  80.  
  81. /*----------------------------------------------------------------------
  82. // pstrcat
  83. //
  84. // Copy one pascal string onto the end of another
  85. ----------------------------------------------------------------------*/
  86. pascal void pstrcat( Str255 dest, Str255 src )
  87. {
  88.     unsigned char*        dp;
  89.     unsigned char*        sp;
  90.     unsigned char*        cp;
  91.     short                i;
  92.     short                max;
  93.     
  94.     sp = (unsigned char*)src;
  95.     dp = (unsigned char*)dest;
  96.     max = *sp;
  97.     /*
  98.     // Adjust past length & chars already in dest
  99.     */
  100.     cp = dp + *dp + 1;
  101.     ++sp;
  102.     /*
  103.     // Determine how many characters we can really copy
  104.     */
  105.     if( max + *dp > 255 )
  106.         max = 255 - *dp;
  107.     *dp += max;
  108.     
  109.     for( i = 0; i < max ; ++i )
  110.         *cp++ = *sp++;
  111. }
  112.  
  113. /*----------------------------------------------------------------------
  114. // pstrcmp
  115. //
  116. // Compare two Pascal strings
  117. ----------------------------------------------------------------------*/
  118. short pstrcmp( unsigned char* a, unsigned char* b )
  119. {
  120.     short        len = ( *a < *b ? *a : *b );
  121.     short        lenEqual = ( *a > *b ) - ( *a < *b );
  122.     short        equalityTest;
  123.     
  124.     while( len )
  125.     {
  126.         ++a;
  127.         ++b;
  128.         equalityTest = ( *a > *b ) - ( *a < *b );
  129.         
  130.         if( equalityTest )
  131.             return equalityTest;
  132.         
  133.         --len;
  134.     }
  135.     
  136.     return lenEqual;
  137. }
  138.  
  139. /*-------------------------------------------------------------------
  140. // partialstrcmp
  141. //
  142. // 'Partial' string compare - check if s2 appears at the beginning
  143. // of s1.
  144. //
  145. // Identical to 'strncmp(s1,s2,strlen(s2));', with the bonus
  146. // feature of being case-insensitive.
  147. -------------------------------------------------------------------*/
  148. short partialstrcmp( register char* s1, register char* s2)
  149. {
  150.     register char    c1,
  151.                     c2;
  152.     
  153.     while(    (c2 = *s2++) != 0 )
  154.     {
  155.         c1 = *s1++;
  156.         c1 = ToUpper(c1);
  157.         c2 = ToUpper(c2);
  158.         if( c1 < c2 ) return(-1);
  159.         if( c1 > c2 ) return( 1);
  160.     }
  161.     return(0);
  162. }
  163.  
  164. /*-------------------------------------------------------------------
  165. // ScanNumberInString
  166. //
  167. // Convert an ascii number into a string, advance the string ptr
  168. // past the number
  169. -------------------------------------------------------------------*/
  170. short ScanNumberInString( char** line )
  171. {
  172.     short n = 0;
  173.     while( (**line >= '0') && (**line <= '9') )
  174.     {
  175.         n = n * 10 + (**line - '0');
  176.         ++(*line);
  177.     }
  178.     return n;
  179. }
  180.  
  181. /*-------------------------------------------------------------------
  182. // NumberInString
  183. //
  184. // Convert an ascii number into a string
  185. -------------------------------------------------------------------*/
  186. short NumberInString( char* line )
  187. {
  188.     return( ScanNumberInString( &line ) );
  189. }
  190.  
  191. /*-------------------------------------------------------------------
  192. // ScanTextInString
  193. //
  194. // Copy characters from **line into the specified pstr
  195. -------------------------------------------------------------------*/
  196. void ScanTextInString( char** line, Str255 pstr, Boolean terminateAtSpace )
  197. {
  198.     short        len = 0;
  199.     
  200.     while( (len < 256) && ( (**line > ' ') || ( (**line == ' ') && (terminateAtSpace == false)) ) )
  201.     {
  202.         ++len;
  203.         pstr[len] = **line;
  204.         (*line)++;
  205.     }
  206.     pstr[0] = len;
  207. }
  208.  
  209. /*-------------------------------------------------------------------
  210. // ScanWordInString
  211. //
  212. // Copy the next word from **line into the specified pstr
  213. -------------------------------------------------------------------*/
  214. void ScanWordInString( char** line, Str255 pstr )
  215. {
  216.     ScanTextInString( line, pstr, true );
  217. }
  218.  
  219. /*-------------------------------------------------------------------
  220. // ScanLineInString
  221. //
  222. // Copy the next line from **line into the specified pstr
  223. -------------------------------------------------------------------*/
  224. void ScanLineInString( char** line, Str255 pstr )
  225. {
  226.     ScanTextInString( line, pstr, false );
  227. }
  228.  
  229. /*-------------------------------------------------------------------
  230. // AddHexCharToString
  231. //
  232. // Add a hex character to a string.  Always adds 1 character
  233. -------------------------------------------------------------------*/
  234. void AddHexCharToString( char* where, unsigned short hexNumber )
  235. {
  236.     hexNumber &= 0xF;
  237.     
  238.     if( hexNumber < 10 )
  239.         *where = '0' + hexNumber;
  240.     else
  241.         *where = 'A' + (hexNumber - 10);
  242. }
  243.  
  244. /*-------------------------------------------------------------------
  245. // AddHexByteToString
  246. //
  247. // Add a hex byte to a string.  Always adds 2 characters
  248. -------------------------------------------------------------------*/
  249. void AddHexByteToString( char* where, unsigned short hexNumber )
  250. {
  251.     AddHexCharToString( where, hexNumber >> 4 );
  252.     AddHexCharToString( where + 1, hexNumber & 0xF );
  253. }
  254.  
  255. /*-------------------------------------------------------------------
  256. // AddHexShortToString
  257. //
  258. // Add a hex shortword to a string.  Always adds 4 characters
  259. -------------------------------------------------------------------*/
  260. void AddHexShortToString( char* where, unsigned short hexNumber )
  261. {
  262.     AddHexByteToString( where, hexNumber >> 8 );
  263.     AddHexByteToString( where + 2, hexNumber & 0xFF );
  264. }
  265.  
  266. /*-------------------------------------------------------------------
  267. // AddHexLongToString
  268. //
  269. // Add a hex longword to a string.  Always adds 8 characters
  270. -------------------------------------------------------------------*/
  271. void AddHexLongToString( char* where, unsigned long hexNumber )
  272. {
  273.     AddHexShortToString( where, hexNumber >> 16 );
  274.     AddHexShortToString( where + 4, hexNumber & 0xFFFF );
  275. }
  276.